package xwbank.jmeter5.scripts.sofa;

import com.alipay.sofa.rpc.config.ApplicationConfig;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import xwbank.jmeter5.scripts.sofa.service.CmpServiceSingleton;

public class LoanResultNoticeTest extends AbstractJavaSamplerClient {

    private static CmpService cmpService = null;
    private PayResultNotifyRequest payResultNotifyRequest = null;
    private CmpResponse cmpResponse = null;

    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.addArgument("loanNo","");
        return arguments;
    }

    @Override
    public void setupTest(JavaSamplerContext context) {
        cmpService = CmpServiceSingleton.getInstance().getCmpService();
        payResultNotifyRequest = new PayResultNotifyRequest();
        cmpResponse = new CmpResponse();
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        String loanNo = javaSamplerContext.getParameter("loanNo");

        payResultNotifyRequest.setLoanNo(loanNo);

        SampleResult sampleResult = new SampleResult();
        sampleResult.sampleStart();

        cmpResponse = cmpService.loanResultNotice(payResultNotifyRequest);

        if (cmpResponse.getValue().toString().contains("SUCCESS")) {
            sampleResult.setSuccessful(true);
            sampleResult.setResponseHeaders("response: "+JSON.toJSONString(cmpResponse));
            sampleResult.setRequestHeaders("request: " + JSON.toJSONString(payResultNotifyRequest));
            sampleResult.setDataType(SampleResult.TEXT);
        }else {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseHeaders("response: "+JSON.toJSONString(cmpResponse));
            sampleResult.setRequestHeaders("request: " + JSON.toJSONString(payResultNotifyRequest));
            sampleResult.setDataType(SampleResult.TEXT);
        }
        sampleResult.sampleEnd();
        return sampleResult;
    }

    @Override
    public void teardownTest(JavaSamplerContext context) {
        super.teardownTest(context);
    }
}
